package server;

import container.Mapper;

import java.io.InputStream;
import java.net.Socket;

public class RequestProcessor implements Runnable {

    private Socket socket;


    public RequestProcessor(Socket socket) {
        this.socket = socket;
    }


    @Override
    public void run() {
        try {
            InputStream inputStream = socket.getInputStream();

            // 封装Request对象和Response对象
            Request request = new Request(inputStream);
            Response response = new Response(socket.getOutputStream());

            HttpServlet httpServlet = Mapper.findServlet(request.getHost(),request.getPort(),request.getUrl());
            if (httpServlet == null){
                response.outputHtml(request.getUrl());
            }else {
                httpServlet.service(request,response);
            }

            socket.close();

        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
